/*
*  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
*  Copyright (C)  2012. Ian Quigley
*  
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*  
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*/


var cssTool = new function () {

	this.condense = function ( css ) {
		
		if (typeof CSSJSON === undefined) {
			return "Missing CSSJSON library";
		}
		
		return CSSJSON.toCSS( condenseCssJson( CSSJSON.toJSON( css ) ) );		
	}

	// Remove redundand CSS junk (usually as a by-product of jQuery)
	function condenseCssJson( json ) {

		// Remove items with value 'initial' and 'initial initial'
		$.each( json, function (i, item) { if (item === 'initial') delete json[i]; })
		$.each( json, function (i, item) { if (item === 'initial initial') delete json[i]; })
			
		// Convert "rgb(r,g,b)" into "#rrggbb"
		$.each( json, function (i, item) { 
			if (item.length > 7 && (item.substr(0,4) === 'rgb(')) {
					json[i] = cssTool.rgbToHex(cssTool.decodeARGB(json[i]));
				}	
			});	
		
		// background-color: rgba( 0, 0, 0, 0) is redundant
		if ((json["background-color"] !== undefined) && cssTool.isTransparent(cssTool.decodeARGB(json["background-color"]))) 
			delete json["background-color"];
		
		// Reduce border-xxx-width/style/color to single border: width style color;
		reduceBorderSide(json, 'border-left');
		reduceBorderSide(json, 'border-top');
		reduceBorderSide(json, 'border-right');
		reduceBorderSide(json, 'border-bottom');
		
		// Reduce to single border statement if left=right=top=bottom
		reduceBorderSingle( json );
		
		return json;
	};

	function reduceBorderSingle( json ) {

		if ((json["border-left"] !== undefined) && (json["border-right"] !== undefined) && (json["border-top"] !== undefined) && (json["border-bottom"] !== undefined))
		{
			if ( (json["border-left"] === json["border-right"]) && (json["border-left"] === json["border-top"]) && (json["border-left"] === json["border-bottom"]))
			{
				json["border"] = json["border-left"];
				delete json["border-left"];
				delete json["border-right"];
				delete json["border-top"];
				delete json["border-bottom"];
			}
		}
		
		return json;
	};
	
	function reduceBorderSide( json, prefix ) {
		
		if ( (json[prefix + '-width'] !== undefined) && (json[prefix + '-style'] !== undefined) && (json[prefix + '-color'] !== undefined))
		{
			json[prefix] = json[prefix + '-width'] + ' ' + json[prefix + '-style']  + ' ' + json[prefix + '-color'];
			delete json[prefix + '-width'];
			delete json[prefix + '-style'];
			delete json[prefix + '-color'];
		}
		
		return json;
	};

	this.decodeARGB = function (rgb) {
		
		var obj = new Object();
		
		// from http://stackoverflow.com/questions/638948/background-color-hex-to-javascript-variable-jquery
		var parts = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
		if (parts === null)
			parts = rgb.match(/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\d+)\)$/);

		if (parts === null)
			return null;
			
		var alpha = 0;
		if (parts.length === 5) {	
			alpha = 1;
			obj.a = 1 * parts[1];
		}
		
		obj.r = 1 * parts[alpha + 1];
		obj.g = 1 * parts[alpha + 2];
		obj.b = 1 * parts[alpha + 3];	
		
		return obj;
	};

	this.rgbToHex = function(rgb) {	
		return '#' + hex(rgb.r) + hex(rgb.g) + hex(rgb.b);	
	};

	this.brightness = function( rgb, percent ) {

		rgb.r += ((rgb.r * percent)/100);
		rgb.g += ((rgb.g * percent)/100);
		rgb.b += ((rgb.b * percent)/100);
		
		rgb.r = Math.floor(rgb.r);
		rgb.g = Math.floor(rgb.g);
		rgb.b = Math.floor(rgb.b);	
	
		if (rgb.r > 255) rgb.r = 255;
		if (rgb.g > 255) rgb.g = 255;
		if (rgb.b > 255) rgb.b = 255;	
		if (rgb.r < 0) rgb.r = 0;
		if (rgb.g < 0) rgb.g = 0;
		if (rgb.b < 0) rgb.b = 0;
		
		return rgb;
	};

	this.average = function( rgb ) {
		return Math.floor( rgb.r + rgb.b + rgb.g / 3 );
	};

	this.isTransparent = function( argb ) {
		return (argb !== null && argb !== undefined && argb.a === 0);
	};

	function hex(x) {
	  return ("0" + parseInt(x).toString(16)).slice(-2);
	 };
};